
import tkinter as tk
from tkinter import filedialog, simpledialog

class Teleprompter:
    def __init__(self, root):
        self.root = root
        self.root.title("Python提词器")
        self.text = ""
        self.speed = 1000  # 默认滚动速度（毫秒）
        self.position = 0
        
        # 界面组件
        self.text_area = tk.Text(root, wrap='word', font=('Arial', 24), bg='black', fg='white')
        self.text_area.pack(expand=True, fill='both')

        self.sel=0


        # 配置标签样式
        self.text_area.tag_config("highlight", background="green")

        
        # 控制面板
        control_frame = tk.Frame(root)
        control_frame.pack(fill='x')
        
        tk.Button(control_frame, text="导入文本", command=self.load_file).pack(side='left')
        tk.Button(control_frame, text="设置速度", command=self.set_speed).pack(side='left')
        tk.Button(control_frame, text="开始/暂停", command=self.toggle_scroll).pack(side='left')
        tk.Button(control_frame, text="全屏", command=self.toggle_fullscreen).pack(side='left')
        
        self.scroll_id = None

    def load_file(self):
        file_path = filedialog.askopenfilename(filetypes=[("Text files", "*.txt")])
        if file_path:
            with open(file_path, 'r', encoding='utf-8') as file:
                self.text = file.read()
            self.text_area.delete(1.0, tk.END)
            self.text_area.insert(tk.END, self.text)
            self.position = 0

    def set_speed(self):
        speed = simpledialog.askinteger("设置速度", "输入滚动速度(毫秒):", initialvalue=self.speed)
        if speed:
            self.speed = speed
            if self.scroll_id:
                self.root.after_cancel(self.scroll_id)
                self.start_scrollEx()

    def toggle_scroll(self):
        if self.scroll_id:
            self.root.after_cancel(self.scroll_id)
            self.scroll_id = None
        else:
            self.start_scrollEx()

    def start_scroll(self):
        self.text_area.yview_moveto(self.position)

        self.position += 0.001  # 滚动步长
        if self.position >= 1.0:
            self.position = 0
        self.scroll_id = self.root.after(self.speed, self.start_scroll)

    
    def start_scrollEx(self):
        # self.text_area.yview_moveto(self.position)
        # 给第1行第0字符到第1行第5字符添加标签
        # self.text_area.tag_add("highlight", "1.0", f"{self.position}.8")


        self.position += 0.01  # 滚动步长
        rownum=int(self.position*115)
        self.sel += 5
        if rownum%10==0:
            self.text_area.yview_moveto(self.position)
        # self.text_area.tag_add("highlight", "1.0", f"{rownum}.100") 
        self.text_area.tag_add("highlight", "1.0", f"{self.sel}") 

        if self.position >= 1.0:
            self.position = 0
            self.text_area.selection_clear()
        self.scroll_id = self.root.after(self.speed, self.start_scrollEx)


    def toggle_fullscreen(self):
        self.root.attributes('-fullscreen', not self.root.attributes('-fullscreen'))

if __name__ == "__main__":
    root = tk.Tk()
    app = Teleprompter(root)
    root.mainloop()
